#include<stdio.h>
#include<string.h>
#include<windows.h>

int check_parenthese(char * s)
{
    int len  = strlen(s);

    // 简陋的栈
    char* stack = (char*) malloc(len);
    int stk_top = 0;

    for(int i = 0; i < len; ++ i)
    {
        switch (s[i])
        {

        // 左括号
        case '(':
            stack[stk_top] = '(';                                    
            ++ stk_top;
            break;

        case '[':        
            stack[stk_top] = '[';                                    
            ++ stk_top;
            break;

        case '{':
            stack[stk_top] = '{';                                    
            ++ stk_top;
            break;

        // 右括号
        case ')':
            if(stk_top == 0 || stack[stk_top-1] != '(')
            {
                return 0;
            }
            -- stk_top;
            break;

        case ']':
            if(stk_top == 0 || stack[stk_top-1] != '[')
            {
                return 0;
            }
            -- stk_top;
            break;

        case '}':
            if(stk_top == 0 || stack[stk_top-1] != '{')
            {
                return 0;
            }
            -- stk_top;
            break;


        default:
            break;
        }
    }

    // 判断有没有多余左括号
    if(stk_top != 0)
    {
        return 0;
    }

    return 1;

} // 检查括号对齐的算法

int main ()
{
    printf("Please Type the String in one line:\n");

    //读取1023个字节
    char s[1024] = {0};
    fgets(s,1023,stdin);
    
    if(check_parenthese(s))
    {
        printf("PASS\n");
    }
    else
    {
        printf("Fail\n");
    }
    printf("press enter to quit...");
    getchar();
    return 0;
}